package com.sat.learning.Views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Project:Projects
 * Package:com.sat.learning.Views
 * Created by 透野君爱打奶炮 on 2018-03-22 at 18:54
 */
public class SlideMenu extends ViewGroup {

    private static final int MAIN_MODE = 0;
    private static final int MENU_MODE = 1;
    private int currentMode = MAIN_MODE;
    private float downX;
    private float moveX;
    private Scroller scroller;
    private float downY;
    private float moveY;

    public SlideMenu(Context context) {
        this(context, null);
    }
    public SlideMenu(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        scroller = new Scroller(getContext());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        View leftView = getChildAt(0);
        leftView.measure(leftView.getLayoutParams().width,heightMeasureSpec);

        View rightView = getChildAt(1);
        rightView.measure(widthMeasureSpec,heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View leftView = getChildAt(0);
        leftView.layout(-leftView.getMeasuredWidth(),0,0,b);
        getChildAt(1).layout(l,t,r,b);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = event.getX();
                float offset = downX - moveX;
                float scrollToX = getScrollX() + offset;
                if (scrollToX<-getChildAt(0).getMeasuredWidth()){
                    scrollTo(-getChildAt(0).getMeasuredWidth(),0);
                }else if (scrollToX>0){
                    scrollTo(0,0);
                }else{
                    scrollBy((int) offset,0);
                }
                downX = moveX;
                break;
            case MotionEvent.ACTION_UP:
                if (getScrollX()>-getChildAt(0).getMeasuredWidth()*0.5f){
                    //主界面
//                    scrollTo(0,0);
                    currentMode = MAIN_MODE;
                    updateCurrentMode();
                }else {
                    //左菜单
//                    scrollTo(-getChildAt(0).getMeasuredWidth(),0);
                    currentMode = MENU_MODE;
                    updateCurrentMode();
                }
                break;
        }
        return true;
    }

    private void updateCurrentMode() {
        int startX = getScrollX();
        int dx = 0;
        if (currentMode == MAIN_MODE){
            //主界面
            dx = -startX;
        }else {
            //菜单
            dx = -getChildAt(0).getMeasuredWidth()-startX;
        }

        int duration = Math.abs(dx * 5);
        scroller.startScroll(startX,0,dx,0,duration);
        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()){
            int currX = scroller.getCurrX();
            scrollTo(currX,0);
            invalidate();
        }
    }

    public void switchMode(){
        if (currentMode == MAIN_MODE){
            currentMode=MENU_MODE;
        }else {
            currentMode = MAIN_MODE;
        }
        updateCurrentMode();
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = ev.getX();
                moveY = ev.getY();
                float xOffset = Math.abs(moveX-downX);
                float yOffset = Math.abs(moveY-downY);
                if (yOffset<xOffset&&xOffset>50){
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
